package vn.igolf.golf;

import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;

public class Hole {

	private String name;
	private int par;
	private ArrayList<Point> tees = new ArrayList<Point>();
	
	private static String NAME = "name";
	private static String PAR = "par";
	private static String TEE_POSITION = "tee position";
	private static String PIN_POSITION = "pin posision";
	
	public Hole (){
	}
	
	private Point indicatorPoint;
	private int length;
	
	public static Hole readHoleFromFile(BufferedReader br) throws IOException {
		Hole hole = new Hole();
        String line = null;
        while ((line=br.readLine()) != null) {
        	line = line.trim().toLowerCase();
        	if(line.length()==0) continue;
        	if(line.charAt(0)!='#') continue;
        	line = line.substring(1).trim();
        	if(line.equals(NAME)) hole.readNameFromFile (br);
        	if(line.equals(PAR)) hole.readParFromFile (br);
        	if(line.equals(TEE_POSITION)) hole.readTeesFromFile (br);
        	if(line.equals(PIN_POSITION)) {
        		hole.readTeesFromFile (br);
        		break;
        	}
        }
        
		return hole;
	}

	private void readTeesFromFile(BufferedReader br) throws IOException {
		String line = null;
		while ((line =br.readLine())!=null) {
			line = line.trim();
			if (line.length()==0) break;
			addTee(line);
		}
	}
	
	private void addTee(String s) {
		String[] ss = s.split(" ");
		Point p = new Point (new Integer(ss[0]), new Integer(ss[1]));
		tees.add(p);
		if (ss.length>2) {
			length = new Integer (ss[2]);
			indicatorPoint = p;
		}
	}

	private void readParFromFile(BufferedReader br) throws NumberFormatException, IOException {
		setPar( new Integer(br.readLine().trim()) );		
	}

	private void readNameFromFile(BufferedReader br) throws IOException {
		setName( br.readLine().trim() );		
	}

	public String toString () {
		StringBuffer sb = new StringBuffer();
		sb.append(name + "\n");
		sb.append(par + "\n");
		sb.append(tees + "\n");
		sb.append(indicatorPoint+ "\n");
		sb.append(length+ "\n");
		return sb.toString();
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPar() {
		return par;
	}

	public void setPar(int par) {
		this.par = par;
	}

	public ArrayList<Point> getTees() {
		return tees;
	}

	public void setTees(ArrayList<Point> tees) {
		this.tees = tees;
	}

}
